package com.yizhi.student.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yizhi.common.utils.R;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import com.yizhi.student.dao.StudentInfoDao;
import com.yizhi.student.domain.StudentInfoDO;
import com.yizhi.student.service.StudentInfoService;



@Service
public class StudentInfoServiceImpl implements StudentInfoService {



	@Autowired
	private StudentInfoDao studentInfoDao;
	
	@Override
	public StudentInfoDO get(Integer id){
		System.out.println("======service层中传递过来的id参数是：" + id + "======");
		return studentInfoDao.get(id);
	}


	@Override
	public List<StudentInfoDO> list(Map<String, Object> map){
		int pageSize = Integer.parseInt(map.get("pageSize").toString());
		int offset = (Integer.parseInt(map.get("currPage").toString())-1)*pageSize;
//		System.out.println("***********************************"+offset);
		HashMap<String, Integer> stringIntegerHashMap = new HashMap<>();
		stringIntegerHashMap.put("pageSize",pageSize);
		stringIntegerHashMap.put("offset",offset);
		map.put("hmap",stringIntegerHashMap);
		return studentInfoDao.list(map);
	}


	@Override
	public int count(Map<String, Object> map){
		return studentInfoDao.count(map);
	}

	/**
	 * 工具方法
	 */
	public static ArrayList<String> getOneColumns(){
		//		获取字段名
		List<String> columns = Arrays.asList("student_id", "exam_id", "certify", "card_id", "telephone");
		ArrayList<String> s1 = new ArrayList<>(columns);
		return s1;
	}
	public static ArrayList<String> getOneStrs(StudentInfoDO studentInfo){
		//		获取待修改字段值
		List<String> providedStrs = Arrays.asList(studentInfo.getStudentId(), studentInfo.getExamId(), studentInfo.getCertify(),
				studentInfo.getCardId(), studentInfo.getTelephone());
		ArrayList<String> s2 = new ArrayList<>(providedStrs);
		return s2;
	}
	@Override
	/**
	 * 保存也要做唯一字段控制
	 */
	public R save(StudentInfoDO studentInfo){
		ArrayList<String> s1 = getOneColumns();
		ArrayList<String> s2 = getOneStrs(studentInfo);
		//唯一性检查
		for (int i = 0; i < 5; i++) {
			if(studentInfoDao.getOne(s1.get(i),s2.get(i))!=0){//唯一性检查，不为0就一定冲突
				return R.error("学号、考生号、身份证号、一卡通和手机号之一与其他同学冲突了！请重写检查输入！");
			}
		}
		studentInfoDao.save(studentInfo);
		return R.ok();
	}
	
	@Override
	/**
	 * 修改要做控制：学号、考生号、身份证号、一卡通和手机号五个字段是唯一的，不可重复
	 */
	public R update(StudentInfoDO studentInfo){
		ArrayList<String> s1 = getOneColumns();
		ArrayList<String> s2 = getOneStrs(studentInfo);
//		唯一性检查
		for (int i = 0; i < 5; i++) {
			if(studentInfoDao.getOne(s1.get(i),s2.get(i))!=0){//唯一性检查，包含了修改时冲突和本身未修改两者情况
				if(studentInfoDao.getOneId(s1.get(i),s2.get(i)) == studentInfo.getId()){//本身未修改
					continue;
				}
				//修改时冲突
				return R.error("学号、考生号、身份证号、一卡通和手机号之一与其他同学冲突了！请重写检查输入！");
			}
		}
		studentInfoDao.update(studentInfo);
		return R.ok();
	}
	
	@Override
	public int remove(Integer id){
		return studentInfoDao.remove(id);
	}
	
	@Override
	public int batchRemove(Integer[] ids){
		return studentInfoDao.batchRemove(ids);
	}
	
}
